package com.example.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.jline.reader.Parser;
import org.springframework.shell.Shell;
import org.springframework.shell.jline.FileInputProvider;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.commands.Script;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

@Slf4j
@ShellComponent
@ShellCommandGroup("system ctl")
public class ScriptCommand implements Script.Command{

    private final Shell shell;

    private final Parser parser;

    public ScriptCommand(Shell shell, Parser parser) {
        this.shell = shell;
        this.parser = parser;
    }

    @ShellMethod(value = "Read and execute commands from a file.")
    public void script(File file) throws IOException {
        Reader reader = new FileReader(file);
        try (FileInputProvider inputProvider = new FileInputProvider(reader, parser)) {
            shell.run(inputProvider);
        }
    }

}



